class WithDefault():
    def __init__(self, key_validator, default=None):
        self._internal = {}
        self._default = default
        self._key_validator = key_validator
        
    def __getitem__(self, key):
        if self._internal.has_key(key):
            return self._internal[key]
        else:
            return self._default
        
    def __setitem__(self, key, value):
        if self[key] is not value:
            self._internal[key] = value
        
    def has_key(self, key):
        if self._internal.has_key(key):
            return True
        else:
            return self._key_validator(key)
        
    def clear(self):
        self._internal.clear()
